package com.lfg.leetcode.leetcode1000;


import com.lfg.leetcode.leetcode0.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class No1161maxLevelSum {
    public static void main(String[] args) {
        int ret = maxLevelSum(TreeNode.stringToTreeNode("[1,7,0,7,-8,null,null]"));
        System.out.println(ret);
    }

    public static int maxLevelSum(TreeNode root) {
        List<TreeNode> children = new ArrayList<>();
        children.add(root);

        int ret = 0;
        int cnt = Integer.MIN_VALUE;
        int level = 0;
        do {
            level++;
            List<TreeNode> next = new ArrayList<>();
            int total = 0;
            for (TreeNode node : children) {
                total+= node.val;
                if (node.left != null) {
                    next.add(node.left);
                }
                if (node.right != null) {
                    next.add(node.right);
                }
            }
            if(total> cnt){
                cnt = total;
                ret = level;
            }
            children = next;
        } while (!children.isEmpty());

        return ret;
    }

}



